<?php
declare (strict_types = 1);

namespace app\validate;

use think\Validate;

class AdminStaff extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'student_no|学号' => 'max:50',
        'name|姓名' => 'require|max:20',
        'sex|性别' => 'require',
        'id_card|身份证' => 'idCard',
        'phone|手机号' => 'mobile',
        'password|密码' => 'requireCallback:check_password|length:6,12',
        'school_id|学校' => 'require|integer',
        'entrance_time|入学时间' => 'dateFormat:Y-m|lt:graduate_time',
        'graduate_time|毕业时间' => 'dateFormat:Y-m|gt:entrance_time'
    ];

    protected $message = [
        'student_no.requireWithout' => '学号和手机号不能都为空',
        'phone.requireWithout' => '手机号和学号不能都为空',
        'entrance_time.require' => '入学时间不能为空',
        'graduate_time.require' => '毕业时间不能为空',
        'password.length' => '密码长度不符合要求,请输入6-12位密码',
        'entrance_time.lt' => '入学时间必须小于毕业时间',
        'graduate_time.gt'     => '毕业时间必须大于入学时间',
    ];

    function check_password($value, $data){
        if(empty($value)){
            return false;
        } else {
            return true;
        }
    }

    public function sceneV3Edit()
    {
        return $this->only(['avatar_url','name','sex','entrance_time','graduate_time'])
            ->append('entrance_time', 'require')
            ->append('graduate_time', 'require')
            ->append('student_no', 'requireWithout:phone')
            ->append('phone', 'requireWithout:student_no');
    }

    public function sceneEdit()
    {
        return $this->only(['avatar_url','name','sex', 'id_card','uuid'])
            ->append('uuid|用户标识', 'require');
    }

    // 后台学生添加校验
    public function sceneStudent()
    {
        return $this->only(['school_id','student_no','name','sex', 'phone','password','entrance_time','graduate_time'])
            ->append('student_no', 'requireWithout:phone')
            ->append('phone', 'requireWithout:student_no')
            ->append('major', 'require');
    }

    // 后台职工添加校验
    public function sceneStaff()
    {
        return $this->only(['school_id','name','sex', 'phone','password','type'])
            ->append('phone', 'require')
            ->append('type|职位', 'require|in:1,2,3,4,5,6');
    }
}
